#ifdef WIN32
#include <stdio.h>
#include <mdshow.hpp>
#include <windows.h>

wchar_t *
convert_filename(const char *str)
{
    wchar_t      *filename = NULL;
    unsigned int len       = 0;
    unsigned int converted = 0;

    len      = strlen(str) + 1;
    filename = new wchar_t[len];

    mbstowcs_s(&converted, filename, len, str, len);

    return filename;
}

HRESULT
find_crossbar_pin(IAMCrossbar *pXBar,
                  int pType,
                  PIN_DIRECTION Dir,
                  long *pIndex)
{
    HRESULT result = E_FAIL;
    BOOL    bInput = FALSE;
    long    cOut   = 0;
    long    cIn    = 0;
    long    count  = 0;

    bInput = (Dir == PINDIR_INPUT ? TRUE : FALSE);
    result = pXBar->get_PinCounts(&cOut, &cIn);

    if (FAILED(result))
        return result;

    count = (bInput) ? cIn : cOut;
    for (long i = 0; i < count; i++) {
        long    iRelated = 0;
        long    physical = 0;

        result = pXBar->get_CrossbarPinInfo(bInput, i, &iRelated, &physical);
        if (SUCCEEDED(result) && physical == pType) {
            *pIndex = i;
            return S_OK;
        }
    }

    return E_FAIL;
}

HRESULT
route_crossbar(IAMCrossbar *pXBar, int pType)
{
    long    i      = 0;
    HRESULT result = E_FAIL;

    result = find_crossbar_pin(pXBar,
                               PhysConn_Video_VideoDecoder,
                               PINDIR_OUTPUT,
                               &i);

    if (SUCCEEDED(result)) {
        long    j = 0;

        result = find_crossbar_pin(pXBar, pType, PINDIR_INPUT, &j);

        if (SUCCEEDED(result))
            return pXBar->Route(i, j);
        else
            return pXBar->Route(i, -1);
    }
    return E_FAIL;
}
#endif

#include <string.h>
#include <stdlib.h>

#ifdef WIN32
#define GCHART TCHAR
#else
#define GCHART char
#endif

GCHART *
mstrdup(const char *str)
{
    if (!str)
        return NULL;

    size_t      len;
    GCHART      *out;

    len = strlen(str) + 1;
    out = new GCHART[len];

#ifdef WIN32
    size_t      converted;
    mbstowcs_s(&converted, out, len - 1, str, len - 1);
#else
    memcpy(out, str, len - 1);
#endif
    out[len - 1] = '\0';

    return out;
}
